//go:build !prod
// +build !prod

package application

import (
	"github.com/energye/energy/v2/cef"
	"github.com/energye/energy/v2/common"
	"github.com/energye/energy/v2/consts"
	"github.com/energye/energy/v2/pkgs/assetserve"
	"github.com/energye/golcl/pkgs/macapp"
	"path/filepath"
	"webpage-craw/assets"
)

func assetsLoad(app *cef.TCEFApplication) {
	if !common.IsDarwin() {
		// 有 helper 进程时不能指定 BrowserSubprocessPath
		sub := "helper"
		if common.IsWindows() {
			sub += ".exe"
		}
		sub = filepath.Join(consts.CurrentExecuteDir, "src", "helper", sub)
		app.SetBrowserSubprocessPath(sub)
	}
	cef.BrowserWindow.Config.Url = "http://localhost:22022/index.html"
	cef.SetBrowserProcessStartAfterCallback(func(b bool) {
		server := assetserve.NewAssetsHttpServer()
		server.PORT = 22022
		server.LocalAssets = filepath.Join(consts.CurrentExecuteDir, "assets", "resources")
		server.Assets = assets.Assets()
		go server.StartHttpServer()
	})
}

func init() {
	// 开发模式需要手动指定 helper 进程所在目录
	// 在调用 cef.GlobalInit 之前设置
	if common.IsDarwin() {
		subExePath := filepath.Join(consts.CurrentExecuteDir, "src", "helper", "helper")
		macapp.MacApp.SetBrowseSubprocessPath(subExePath)
	}
}
